﻿#include "IconButton.h"

#include <QPainter>


IconButton::IconButton(QWidget *parent, const QIcon &icon, const QString &text, Size size)
    : FToolButton{parent}
    , m_on(icon)
    , m_off(icon)
    , m_isOn(false)
{
    FToolButton::setIcon(icon);
    setAutoRaise(true);

    if (text.isEmpty()) {
        setPopupMode(DelayedPopup);
        setToolButtonStyle(Qt::ToolButtonIconOnly);
    } else {
        setPopupMode(InstantPopup);
        setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    }

    int wh = 16;
    switch (size) {
    case Size::Small:
        wh = 24;
        break;
    case Size::Normal:
        wh = 27;
        break;
    case Size::Large:
        wh = 30;
        break;
    }
    setIconSize(QSize(wh, wh));
}

void IconButton::setOnIcon(const QIcon &icon)
{
    m_on = icon;
}

void IconButton::setOffIcon(const QIcon &icon)
{
    m_off = icon;
}

void IconButton::setIcon(const QIcon &icon)
{
    setOffIcon(icon);
    FToolButton::setIcon(icon);
}

void IconButton::setIconStatus(bool on)
{
    m_isOn = on;
    if (on) {
        FToolButton::setIcon(m_on);
    } else {
        FToolButton::setIcon(m_off);
    }
}

bool IconButton::isOn() const
{
    return m_isOn;
}

void IconButton::setColorIcon(const QColor &color)
{
    m_iconColor = color;

    QPixmap pix(iconSize());
    pix.fill(Qt::transparent);

    QPainter p(&pix);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setBrush(color);
    p.setPen(Qt::black);
    p.drawRect(2, 2, 20, 20);
    p.end();

    setIcon(QIcon(pix));
}

QColor IconButton::iconColor() const
{
    return m_iconColor;
}
